package com.ayi.dao.bookdao;

import com.ayi.bean.Book;
import com.ayi.dao.cartdao.QueryCart;
import com.ayi.utils.CloseConn;
import com.ayi.utils.GetConn;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import static com.ayi.constants.BookShopConstants.*;
import static com.ayi.constants.BookShopConstants.ConstantMethods.*;

/**
 * @author Ayingzz
 * @create 2021/11/28 13:52
 */
public class QueryBook {

    /**
     * 根据用户名和图书id，从购物车里找出某用户购买某本书的数量
     * @param username
     * @param id
     * @return
     */
    public int queryBookNumFromCartByUsernameAndId(String username, int id) {
        StringBuilder sb = new QueryCart().queryCartWithCreateAStringBuilderByUsername(username);
        int[] indexMap = getEndAndStartIndexInCartList(sb, id);
        String middleNum = sb.substring(indexMap[0], indexMap[1]);
        return Integer.parseInt(middleNum);
    }


    /**
     * 查询图书id 以map存储并返回
     * @return
     */
    public Map<Integer, Integer> queryBooksIdForInitEditStatus() {
        Map<Integer, Integer> map = new HashMap<>();
        Connection conn = new GetConn().getConnection();
        String sql = "select * from Books";
        return getInitMap(map, conn, sql);
    }

    /**
     * 根据类型、名字查找图书
     * @param type
     * @return
     */
    public List<Book> queryBooksByTypeAndName(String type, String name) {
        Connection conn = new GetConn().getConnection();
        boolean isAllType = type.equals("全部类型");
        String sql = isAllType ? "select * from Books where name like ?" : "select * from Books where type = ? and name like ?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            if (isAllType) {
                ps.setString(1, "%" + name + "%");
            } else {
                ps.setString(1, type);
                ps.setString(2, "%" + name + "%");
            }
            ResultSet rs = ps.executeQuery();
            return getBooks(rs);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据id查找图书
     * @param bookId
     * @return
     */
    public Book queryBookById(int bookId) {
        Connection conn = new GetConn().getConnection();
        String sql = "select * from Books where id = ?";
        try {
            PreparedStatement ps  = conn.prepareStatement(sql);
            ps.setInt(1, bookId);
            ResultSet rs = ps.executeQuery();
            return getBook(rs);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据id组查找图书
     * @param idGroup
     * @return
     */
    public List<Book> queryBooksByIdGroup(Set<Integer> idGroup, Map<Integer, Integer> numGroup) {
        Connection conn = new GetConn().getConnection();
        List<Book> res = new ArrayList<>();
        String sql = "select * from Books";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                if (idGroup.contains(rs.getInt("id"))) {
                    res.add(getBookWithOutNext(rs));
                }
            }
            return res;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    /**
     *
     * @param username
     * @return
     */
    public List<Book> queryBooksIntoCartSettingNum(String username) {
        Connection conn = new GetConn().getConnection();
        String sql = "select * from Books";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            return getBooksWithSettingNum(rs, username);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
